package com.mybatispuls.springboot.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatispuls.springboot.Utils.R;
import com.mybatispuls.springboot.entity.EduBook;
import com.mybatispuls.springboot.entity.Excel.BookExcel;
import com.mybatispuls.springboot.entity.Vo.BookVo;
import com.mybatispuls.springboot.service.impl.EduBookServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/springboot/book")
@CrossOrigin
public class EduBookController {

    @Autowired
    private EduBookServiceImpl eduBookService;

    //带条件的分页查询
    @PostMapping("pageBook/{current}/{limit}")
    public R pageBook(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) BookVo bookVo){

        Page<EduBook> page = new Page<>(current, limit);
        QueryWrapper<EduBook> queryWrapper = new QueryWrapper<>();
        String bookname = bookVo.getBookname();
        String author = bookVo.getAuthor();
        String press = bookVo.getPress();
        if (!StringUtils.isEmpty(bookname)){
            queryWrapper.like("bookname",bookname);
        }
        if (!StringUtils.isEmpty(author)){
            queryWrapper.like("author",author);
        }
        if (!StringUtils.isEmpty(press)){
            queryWrapper.like("press",press);
        }
        //根据时间排序
        queryWrapper.orderByDesc("gmt_create");
        eduBookService.page(page, queryWrapper);
        long total = page.getTotal();
        List<EduBook> records = page.getRecords();

        return R.ok().data("total",total).data("records",records);
    }
    //添加

    @PostMapping("addBook")
    public R addBook(@RequestBody EduBook eduBook){
        eduBookService.save(eduBook);
        return R.ok();
    }
    //删除
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id){
        boolean b = eduBookService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }

    }

    //修改
    @PostMapping("updateBook")
    public R updateBook(@RequestBody EduBook eduBook){
        eduBookService.updateById(eduBook);
        return R.ok();
    }

    //根据id查询
    @GetMapping("seleteById/{id}")
    public R seleteById(@PathVariable String id){
        EduBook book = eduBookService.getById(id);
        return R.ok().data("book",book);
    }

    //下架列表
    @GetMapping("shelves")
    public R shelves(){
        List<EduBook> book = eduBookService.shelves();
        return R.ok().data("book",book);
    }
    //上架图书
    @PostMapping("shelvesBook/{id}")
    public R shelvesBook(@PathVariable String id){

        eduBookService.upda(id);
        return R.ok();
    }

    //数据导出
    @PostMapping("BookExcel")
    public R BookExcel(){
        List<EduBook> bookList = eduBookService.list(null);
        List<BookExcel> list = new ArrayList<>();
        for (int i = 0; i < bookList.size(); i++) {
            BookExcel excel = new BookExcel();
            EduBook book = bookList.get(i);
            BeanUtils.copyProperties(book,excel);
            list.add(excel);
        }
        String filename = "D:\\image\\book.xlsx";
        EasyExcel.write(filename, BookExcel.class).sheet("图书列表").doWrite(list);
        return R.ok();
    }


}

